package controller;

import java.util.List;

import model.DataHolder;
import model.levels.LevelLoader;
import model.levels.Levels;
import model.surface.Grid;
import model.surface.Map;
import model.surface.PathPoint;
import model.surface.Scale;
import model.utils.PauseMenuDialog;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;

import com.example.towerdefence.R;

/**
 * Representing the game board activity.
 * 
 * @author EvilTeam
 * 
 */
public class GameBoardActivity extends Activity implements OnClickListener {

	private static final int PHONE_SCREEN_WIDTH = 1280;
	private int screenWidth;
	private int screenHeight;
	private GameBoard gameBoard;
	private Levels currentLevel;
	private Button pauseBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setFullScreen();
		setScreenWidthAndHeight();
		Scale.scaleFactory(screenHeight, screenWidth);
		Grid.setGridInstance();

		// Here should get the currentLevel from intent
		currentLevel = Levels.LEVEL1;

		AssetManager assetManager = getResources().getAssets();
		List<PathPoint> pathArray = LevelLoader.readLevelFromJsonFile(
				assetManager, currentLevel.getLevelFileName());
		int backgroundId = currentLevel.getBackgroundId();
		float pathWidth = currentLevel.getPathWidth();

		Map.mapFactory(
				BitmapFactory.decodeResource(getResources(), backgroundId),
				pathArray, pathWidth);
		setContentView(new GameBoard(this));
		if (screenWidth >= PHONE_SCREEN_WIDTH)
			setContentView(R.layout.activity_gameboard_tablets);
		else
			setContentView(R.layout.activity_gameboard);
		gameBoard = new GameBoard(this);

		pauseBtn = (Button) findViewById(R.id.pause);
		pauseBtn.setOnClickListener(this);

		FrameLayout gameBoardLayout = (FrameLayout) findViewById(R.id.FrameLayout);
		gameBoardLayout.addView(gameBoard);
	}

	/**
	 * Sets the current screen width and height to two class variables.
	 */
	private void setScreenWidthAndHeight() {
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		screenWidth = size.x;
		screenHeight = size.y;
	}

	/**
	 * Sets the current activity to full screen.
	 */
	private void setFullScreen() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onPause() {
		super.onPause();
		gameBoard.shutDown();

	}

	@Override
	protected void onResume() {
		super.onResume();
		gameBoard.startUp();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		finish();
		DataHolder.clearData();
		android.os.Process
		.killProcess(android.os.Process.myPid());
	}

	@Override
	public void onClick(View v) {
		PauseMenuDialog.createMsgDialog(GameBoardActivity.this, gameBoard);
	}
}
